package service

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateBillList
//@description: 创建BillList记录
//@param: billList model.BillList
//@return: err error

func CreateBillList(billList model.BillList) (err error, int2 int) {
	id, err := GetLastBillID()
	if err != nil {
		billList.BillId = model.BillIdPrefix + 1
	}else{
		billList.BillId = id + 1
	}
	err = global.GVA_DB.Model(&model.BillList{}).Create(&billList).Error
	return err, billList.BillId
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBillList
//@description: 删除BillList记录
//@param: billList model.BillList
//@return: err error
func DeleteBillList(billList model.BillList) (err error) {
	err = global.GVA_DB.Delete(&billList).Error
	// 同步删除进货单中关联的账单
	if billList.BillRelateGoodsOrderId > 0 {
		db := global.GVA_DB.Model(&model.GoodsOrder{})
		err = db.Where("select_goods_id = ?", billList.BillRelateGoodsOrderId).First(&model.GoodsOrder{}).Update(
			"goods_relate_bill", "").Error
	}
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBillListByIds
//@description: 批量删除BillList记录
//@param: ids request.IdsReq
//@return: err error

//func DeleteBillListByIds(ids request.IdsReq, referrer string) (err error) {
//	if referrer == "admin"{
//		return DeleteBillListByIDS(ids.Ids)
//	}else{
//		var noConfirmBillList []int
//		for _, id := range ids.Ids{
//			_, billInfo := GetBillList(uint(id))
//			if billInfo.BillConfirm != ""{
//				noConfirmBillList = append(noConfirmBillList, id)
//			}
//		}
//		return DeleteBillListByIDS(noConfirmBillList)
//	}
//}
func DeleteBillListByIds(ids request.IdsReq) (err error) {
	// 同步删除进货单中关联的账单
	// 超级注意，在这里因为bug处理了一下午，发现是因为记录已经被删除了，再查询自然没有记录了
	for _, id := range ids.Ids{
		_, billInfo := GetBillList(uint(id))
		if billInfo.BillRelateGoodsOrderId > 0{
			err = global.GVA_DB.Model(&model.GoodsOrder{}).Where("select_goods_id = ?",
				billInfo.BillRelateGoodsOrderId).First(&model.GoodsOrder{}).Update(
				"goods_relate_bill", "").Error
		}
	}
	err = global.GVA_DB.Delete(&[]model.BillList{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateBillList
//@description: 更新BillList记录
//@param: billList *model.BillList
//@return: err error
func UpdateBillList(billList model.BillList) (err error) {
	err = global.GVA_DB.Save(&billList).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBillList
//@description: 根据id获取BillList记录
//@param: id uint
//@return: err error, billList model.BillList
func GetBillList(id uint) (error, model.BillList) {
	var billList model.BillList
	err := global.GVA_DB.Model(&model.BillList{}).Where("id = ?", id).First(&billList).Error
	return err, billList
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBillListInfoList
//@description: 分页获取BillList记录
//@param: info request.BillListSearch
//@return: err error, list interface{}, total int64

func GetBillListInfoList(info request.BillListSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.BillList{})
    var billLists []model.BillList
    // 如果有条件搜索 下方会自动创建搜索语句
	if info.BillUseFor != ""{
		db = db.Where("bill_use_for LIKE ?", "%"+info.BillUseFor+"%")
	}
	if info.BillOperator != ""{
		db = db.Where("bill_operator = ?", info.BillOperator)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&billLists).Error
	return err, billLists, total
}

func ConfirmBill(info model.BillList, referrer string) error {
	if referrer == "JinyuZhang" || referrer == "admin"{
		// 创建db
		db := global.GVA_DB.Model(&model.BillList{})
		var selectLists model.BillList
		err := db.Where("bill_id = ?", info.BillId).First(&selectLists).Update("BillConfirm", referrer).Error
		return err
	}else{
		return fmt.Errorf("Permission denied")
	}
}

func GetLastBillID() (int, error) {
	var billM model.BillList
	db := global.GVA_DB.Model(&model.BillList{})
	err := db.Last(&billM).Error
	return billM.BillId, err
}
